from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker,DeclarativeBase

from common import *
from common.constant import MYSQL_DATABASE, MYSQL_USER, MYSQL_DIALECT, MYSQL_PASSWORD, MYSQL_PORT, MYSQL_HOST


class Base(DeclarativeBase):
    pass


#mysql+mysqldb://<user>:<password>@<host>:<port>/<dbname>
engine = create_engine(
    f"{MYSQL_DIALECT}://{MYSQL_USER}:{MYSQL_PASSWORD}@{MYSQL_HOST}:{MYSQL_PORT}/{MYSQL_DATABASE}?charset=utf8",
    echo=True
)

Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)

def get_session():
    session = Session()
    try:
        yield session
    finally:
        session.close()